package cn.ttxs.lib.wiget.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.WeekView;

/**
 * 演示一个变态需求的周视图
 * Created by huanghaibin on 2018/2/9.
 */

public class CustomWeekView extends WeekView {


    private int mRadius;

    /**
     * 自定义魅族标记的文本画笔
     */
    private Paint mTextPaint = new Paint();


    /**
     * 24节气画笔
     */
    private Paint mSolarTermTextPaint = new Paint();

    /**
     * 背景圆点
     */
    private Paint mPointPaint = new Paint();

    /**
     * 今天的背景色
     */
    private Paint mCurrentDayPaint = new Paint();


    /**
     * 圆点半径
     */
    private float mPointRadius;

    private int mPadding;

    private float mCircleRadius;
    /**
     * 自定义魅族标记的圆形背景
     */
    private Paint mSchemeBasicPaint = new Paint();

    private float mSchemeBaseLine;

    private int colorTextDefault = Color.parseColor("#ff293340");
    private int colorCurrentDay = Color.parseColor("#FF4444");

    public CustomWeekView(Context context) {
        super(context);
        mTextPaint.setTextSize(dipToPx(context, 8));
        mTextPaint.setColor(0xffffffff);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setFakeBoldText(true);


//        mSolarTermTextPaint.setColor(0xff489dff);
//        mSolarTermTextPaint.setAntiAlias(true);
//        mSolarTermTextPaint.setTextAlign(Paint.Align.CENTER);
//
//        mSchemeBasicPaint.setAntiAlias(true);
//        mSchemeBasicPaint.setStyle(Paint.Style.FILL);
//        mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
//        mSchemeBasicPaint.setFakeBoldText(true);
//        mSchemeBasicPaint.setColor(Color.WHITE);
//
//        mPointPaint.setAntiAlias(true);
//        mPointPaint.setStyle(Paint.Style.FILL);
//        mPointPaint.setTextAlign(Paint.Align.CENTER);
//        mPointPaint.setColor(Color.RED);
//
//
//        mCurrentDayPaint.setAntiAlias(true);
//        mCurrentDayPaint.setStyle(Paint.Style.FILL);
//        mCurrentDayPaint.setColor(0xFFeaeaea);
//
//
//        mCircleRadius = dipToPx(getContext(), 7);
//
//        mPadding = dipToPx(getContext(), 3);
//
//        mPointRadius = dipToPx(context, 2);
//
//        Paint.FontMetrics metrics = mSchemeBasicPaint.getFontMetrics();
//        mSchemeBaseLine = mCircleRadius - metrics.descent + (metrics.bottom - metrics.top) / 2 + dipToPx(getContext(), 1);
        mSchemeBasicPaint.setAntiAlias(true);
        mSchemeBasicPaint.setStyle(Paint.Style.FILL);
        mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
        mSchemeBasicPaint.setFakeBoldText(true);
        mSchemeBasicPaint.setColor(Color.WHITE);

        mCurDayTextPaint.setColor(colorCurrentDay);

        mCircleRadius = dipToPx(getContext(), 8);

        mPadding = dipToPx(getContext(), 3);

        mPointRadius = dipToPx(context, 2);

        Paint.FontMetrics metrics = mSchemeBasicPaint.getFontMetrics();
        mSchemeBaseLine = mCircleRadius - metrics.descent + (metrics.bottom - metrics.top) / 2 + dipToPx(getContext(), 1);

        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }


    @Override
    protected void onPreviewHook() {
        mSolarTermTextPaint.setTextSize(mCurMonthLunarTextPaint.getTextSize());
        mRadius = Math.min(mItemWidth, mItemHeight) / 11 * 5;
    }


    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, boolean hasScheme) {
        int cx = x + mItemWidth / 2;
        int cy = mItemHeight / 2;
        canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
        return true;
    }


    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x) {

    }

    @SuppressWarnings("IntegerDivisionInFloatingPointContext")
    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, boolean hasScheme, boolean isSelected) {
        int cx = x + mItemWidth / 2;
        int cy = (int) mTextBaseLine;

        if (hasScheme) {
            String scheme = calendar.getScheme();
            String[] split = scheme.split(",");
            //判断数据的合法性
            if (split != null && split.length == 2) {
                if (!"0".equals(split[0])) {
                    mSchemeBasicPaint.setColor(calendar.getSchemeColor());
                    canvas.drawCircle(x + mItemWidth - mPadding - mCircleRadius / 2, mPadding + mCircleRadius, mCircleRadius, mSchemeBasicPaint);
                    mTextPaint.setColor(Color.WHITE);
                    canvas.drawText(split[1], x + mItemWidth - mPadding - mCircleRadius / 2, mPadding + mSchemeBaseLine, mTextPaint);
                }
            }
        }

        //当然可以换成其它对应的画笔就不麻烦，
        mCurMonthTextPaint.setColor(colorTextDefault);
        mOtherMonthTextPaint.setColor(colorTextDefault);
        if (isSelected) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, cy,
                    mSelectTextPaint);
        } else {
            if (calendar.isCurrentDay()) {
                canvas.drawText(String.valueOf(calendar.getDay()), cx, cy,
                        mCurDayTextPaint);
            } else {
                canvas.drawText(String.valueOf(calendar.getDay()), cx, cy,
                        mOtherMonthTextPaint);
            }
        }

    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
